package com.xxp.controller;

import com.xxp.common.Result;
import com.xxp.controller.request.BookRequest;
import com.xxp.entity.Book;
import com.xxp.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
@CrossOrigin
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/page")
    public Result page(BookRequest bookRequest){
        return Result.success(bookService.listByCondition(bookRequest));
    }

    @PostMapping("/add")
    public Result save(@RequestBody Book book){
        bookService.save(book);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Book book = bookService.getById(id);
        return Result.success(book);
    }
    @DeleteMapping("/{id}")
    public Result deletetById(@PathVariable Integer id){
        bookService.delete(id);
        return Result.success();
    }
    @PutMapping("/update")
    public Result update(@RequestBody Book book){
        bookService.update(book);
        return Result.success();
    }
}
